package com.vjwealth.android.nos.utils

import android.Manifest
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import com.mylhyl.acp.Acp
import com.mylhyl.acp.AcpListener
import com.mylhyl.acp.AcpOptions

/**
 * projectName KNOS
 * date 2017/10/10
 * description com.vjwealth.android.nos.utils
 */
class PhoneUtils{

    companion object {
        private fun toCall(context: Context, phoneNumber: String) {
            val intent = Intent()
            intent.action = Intent.ACTION_CALL
            intent.data = Uri.parse("tel:" + phoneNumber)
            context.startActivity(intent)
        }

        /**
         * 直接拨打电话
         */
        fun intentToCall(context: Context, phoneNumber: String) {
            if (TextUtils.isEmpty(phoneNumber)) return
            if (DeviceUtils.getSDK() >= 23) {
                //6.0权限处理
                Acp.getInstance(context).request(
                        AcpOptions.Builder().setPermissions(Manifest.permission.CALL_PHONE).build(), object : AcpListener {
                    override fun onGranted() {
                        toCall(context, phoneNumber)
                    }

                    override fun onDenied(permissions: List<String>) {
                    }
                })
            } else {
                toCall(context, phoneNumber)
            }
        }
    }
}